package util;

import java.util.StringTokenizer;

public class SequencePredeterminee implements Sequence {

	public final static String NOMBRE_SEQUENCE_PROPERTY_NAME = "sim.sequence.predeterminee";
	private int[] sequence;
	private/* static */ThreadLocal<Integer> actualIndex = new ThreadLocal<Integer>();

	public SequencePredeterminee() {
		actualIndex.set(0);
		String stringSequence = SimProperties.getInstance().getStringProperty(
				NOMBRE_SEQUENCE_PROPERTY_NAME);
		StringTokenizer tokenizer = new StringTokenizer(stringSequence);
		int count = tokenizer.countTokens();
		sequence = new int[count];

		for (int i = 0; i < count; i++) {
			sequence[i] = Integer.parseInt(tokenizer.nextToken());
		}
	}

	@Override
	public synchronized int nextNumber() {
		int retVal;
		retVal = sequence[actualIndex.get()];

		// On recommence la séquence lorsqu'on a terminé.
		actualIndex.set(actualIndex.get() + 1);
		if (actualIndex.get() >= sequence.length) {
			actualIndex.set(0);
		}
		return retVal;
	}

	public static void reset() {
		// actualIndex = 0;
	}

}
